package com.sdgakj.service.notes;

import com.alibaba.fastjson.JSONObject;
import com.sdgakj.entity.Family;

import com.sdgakj.entity.Notes;
import com.sdgakj.entity.Message;
import com.sdgakj.repository.NotesDao;
import net.sf.ehcache.search.expression.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Map;

/**
 * Created by 鲁俊杰 on 2017/4/18.
 * 大事记业务逻辑处理类
 */
@Component
@Transactional
public class NotesService {

    @Autowired
    private NotesDao notesDao;

    public JSONObject getNoteByPage(Map<String, Object> searchParams,
                                    int pageNo, int pageSize,String sortType){
        JSONObject result=new JSONObject();
        Specification<Notes> spec =buildSpecification(searchParams);
        PageRequest pageRequest = buildPageRequest(pageNo,pageSize,sortType);
        Page<Notes> notes = notesDao.findAll(spec, pageRequest);

        result.put("data",notes);
        result.put("pageNo",pageNo);
        result.put("pageSize",pageSize);
        result.put("resMsg", "请求成功!");
        result.put("resCode", "1");

        return result;
    }

    public Page<Notes> getNotesByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
                                    String sortType) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "createtime");

        // Specification<Truck> spec = buildSpecification(searchParams);
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Notes> spec = DynamicSpecifications.bySearchFilter(filters.values(), Notes.class);
        return notesDao.findAll(spec, pageRequest);

    }

    /**
     * 保存Role
     * @param entity
     */
    public JSONObject saveNotes(Notes entity){
        JSONObject result=new JSONObject();
        try{
            notesDao.save(entity);
            result.put("resCode", "1");
            result.put("resMsg", "添加成功");
        }catch (Exception e){
            result.put("resCode", "0");
            result.put("resMsg", "添加失败");
        }
        return result;
    }

    /**
     * 更新数据
     * @param notes
     * @return
     */
    public JSONObject updateNotes(Notes notes){
        JSONObject result=new JSONObject();
        try{
            notesDao.save(notes);
            result.put("resCode", "1");
            result.put("resMsg", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("resCode", "1");
            result.put("resMsg", "修改成功");
        }
        return  result;
    }

    /**
     * 通过id删除大事记
     * @param id
     * @return
     */
    public JSONObject deleteNotesById(Long id){
        JSONObject result=new JSONObject();
        try{
            notesDao.delete(id);
            result.put("resCode", "1");
            result.put("resMsg", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("resCode", "1");
            result.put("resMsg", "删除成功");
        }
        return  result;
    }
    /**
     * 通过id查询Notes对象
     * @param id
     * @return
     */
    public Notes getNoteById(Long id){
        Notes notes=null;
        notes=notesDao.findOne(id);
        return notes;
    }


    /**
     * 创建排序.
     */
    private Sort buildSortRequest(String sortName, String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "id");
        }else if("desc".equals(sortType)){
            sort = new Sort(Sort.Direction.DESC, sortName);
        }else{
            sort = new Sort(Sort.Direction.ASC, sortName);
        }
        return sort;
    }

    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "id");
        }

        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

    /**
     * 创建动态查询条件组合.
     */
    private Specification<Notes> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        //  根据用户类型设置查询条件
        //	filters.put("user.type", new SearchFilter("user.type", Operator.EQ, EnumUserType.CUSTOMOR.getCode()));
        Specification<Notes> spec = DynamicSpecifications.bySearchFilter(filters.values(),Notes.class);
        return spec;
    }
}
